jQuery.fn.extend({
    speed: 600,
    icons: {
        dashminus: 'jicon-dashminus',
        dashplus: 'jicon-dashplus',
        i: 'jicon-i',
        l: 'jicon-l',
        lminus: 'jicon-lminus',
        lplus: 'jicon-lplus',
        minus: 'jicon-minus',
        r: 'jicon-r',
        rminus: 'jicon-rminus',
        rplus: 'jicon-rplus',
        t: 'jicon-t',
        tminus: 'jicon-tminus',
        tplus: 'jicon-tplus'
    },
    entrys: [],
    rootentry: [],
    jTree: function () {
        this.entrys = [];
        this.rootentry = [];
        this.speed = 600;
        $(this).addClass('jtree');
        this.loadJEntry();
        this.setJTree();
        this.setDeep(this.rootentry);
        this.setJIco(this.rootentry);
        return this;
    },
    loadJEntry: function () {
        var _this = this;
        $(this).children().each(function (index, element) {
            var id = $(this).attr('id');
            $(this).addClass('jentry')
            var entry = {
                id: id,
                pid: $(this).attr('pid'),
                element: element,
                deep: 0,
                child: []
            };
            _this.entrys[id] = entry;
        });
    },
    setJTree: function () {
        for (var id in this.entrys) {
            var _this = this.entrys[id];
            var parent = this.entrys[_this.pid];
            if (parent == undefined) {/*根节点*/
                this.rootentry.push(_this);
                continue;
            } else {
                _this.parent = parent;
                parent.child.push(_this);
                if (parent.childpanel == undefined) {
                    parent.childpanel = $('<div></div>').addClass('jchild');
                    $(parent.element).append(parent.childpanel);
                }
                parent.childpanel.append(_this.element);
            }
        }
    },
    setDeep: function (entrys) {
        for (var i = 0; i < entrys.length; i++) {
            var entry = entrys[i];
            if (entry.parent != undefined) {/*根节点*/
                entry.deep = entry.parent.deep + 1;
            }
            $(entry.element).addClass('deep' + entry.deep);
            this.setDeep(entry.child);
        }
    },
    setJIco: function (entrys) {
        var icons = this.icons;
        var _this = this;
        for (var i = 0; i < entrys.length; i++) {
            if (i < entrys.length - 1 && entrys[i].childpanel != undefined) {
                entrys[i].childpanel.addClass(icons.i);
                /*$(entrys[i].element).css('background','url('+icons.i+') left repeat-y');*/
            }
            var ico = $('<span></span>').attr('class', icons.tminus);
            if (entrys[i].deep == 0) {/*根节点*/
                if (entrys.length > 1) {/*多根*/
                    if (i == 0) {/*同级第一个节点*/
                        ico.attr('class', icons.rminus);
                    } else if (i == entrys.length - 1) {/*同级最后一个*/
                        ico.attr('class', icons.lminus);
                    }
                } else {
                    ico.attr('class', icons.dashminus);
                }
            } else if (i == entrys.length - 1) {/*同级最后一个*/
                ico.attr('class', icons.lminus);
            }
            ico.click(function (e) {
                $(this).parent().children('.jchild').children().toggle(_this.speed);
            });
            $(entrys[i].element).prepend(ico);
            this.setJIco(entrys[i].child);
        }
    },
    showEntry: function (entrys, speed) {
        var _this = this;
        for (var i = 0; i < entrys.length; i++) {
            var entry = entrys[i];
            //alert($(this).is(':hidden'))
            $(entry.element).show(speed, function () {
                //alert($(this).html())
                if ($(this).is(':hidden'))$(this).show()
                _this.showEntry(_this.entrys[$(this).attr('id')].child, speed);
            });
        }
    },
    showAll: function (sp) {
        var speed = this.speed;
        if (sp != undefined)
            speed = sp;
        this.showEntry(this.rootentry, speed);
    }
});